package com.hegaoye.account.ctrl;

import com.alibaba.fastjson.JSON;
import com.hegaoye.account.entity.Account;
import com.hegaoye.account.service.AccountSV;
import com.hegaoye.core.entity.BeanRet;
import com.hegaoye.core.entity.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账户 控制器
 *
 * @author pi-top
 */
@RestController
@RequestMapping("/account")
@Slf4j
@Api(value = "账户控制器", description = "账户控制器")
public class AccountCtrl {

  @Resource private AccountSV accountSV;

  /**
   * 查询Account一个详情信息
   *
   * @param id id
   * @param code 编码
   * @param account 账户
   * @return BeanRet
   */
  @ApiOperation(value = "查询Account一个详情信息", notes = "查询Account一个详情信息")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
    @ApiImplicitParam(
        name = "code",
        value = "编码",
        dataType = "java.lang.String",
        paramType = "query"),
    @ApiImplicitParam(
        name = "account",
        value = "账户",
        dataType = "java.lang.String",
        paramType = "query")
  })
  @GetMapping(value = "/load")
  @ResponseBody
  public Account load(java.lang.Long id, java.lang.String code, java.lang.String account) {
    if (id == null) {
      return null;
    }
    if (code == null) {
      return null;
    }
    if (account == null) {
      return null;
    }
    Account accountObj = accountSV.load(id, code, account);
    log.info(JSON.toJSONString(accountObj));
    return accountObj;
  }

  /**
   * 根据条件id查询Account一个详情信息
   *
   * @param id id
   * @return BeanRet
   */
  @ApiOperation(value = "查询Account一个详情信息", notes = "查询Account一个详情信息")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
  })
  @GetMapping(value = "/load/id/{id}")
  @ResponseBody
  public Account loadById(@PathVariable java.lang.Long id) {
    if (id == null) {
      return null;
    }
    Account accountObj = accountSV.loadById(id);
    log.info(JSON.toJSONString(accountObj));
    return accountObj;
  }

  /**
   * 根据条件code查询Account一个详情信息
   *
   * @param code 编码
   * @return BeanRet
   */
  @ApiOperation(value = "查询Account一个详情信息", notes = "查询Account一个详情信息")
  @ApiImplicitParams({
    @ApiImplicitParam(
        name = "code",
        value = "编码",
        dataType = "java.lang.String",
        paramType = "path")
  })
  @GetMapping(value = "/load/code/{code}")
  @ResponseBody
  public Account loadByCode(@PathVariable java.lang.String code) {
    if (code == null) {
      return null;
    }
    Account accountObj = accountSV.loadByCode(code);
    log.info(JSON.toJSONString(accountObj));
    return accountObj;
  }

  /**
   * 根据条件account查询Account一个详情信息
   *
   * @param account 账户
   * @return BeanRet
   */
  @ApiOperation(value = "查询Account一个详情信息", notes = "查询Account一个详情信息")
  @ApiImplicitParams({
    @ApiImplicitParam(
        name = "account",
        value = "账户",
        dataType = "java.lang.String",
        paramType = "path")
  })
  @GetMapping(value = "/load/account/{account}")
  @ResponseBody
  public Account loadByAccount(@PathVariable java.lang.String account) {
    if (account == null) {
      return null;
    }
    Account accountObj = accountSV.loadByAccount(account);
    log.info(JSON.toJSONString(accountObj));
    return accountObj;
  }

  /**
   * 查询Account信息集合
   *
   * @return 分页对象
   */
  @ApiOperation(
      value = "查询Account信息集合",
      notes =
          "查询Account信息集合，需要请求方式请用POST，Ajax的[Content-Type: application/json;charset=utf-8]模式，并对以下属性进行设置，最后以JSON格式提交")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query")
  })
  @PostMapping(value = "/list")
  @ResponseBody
  public List<Account> list(
      @RequestBody @ApiIgnore Account accountObj, Integer curPage, Integer pageSize) {
    Page<Account> page = new Page<Account>(pageSize, curPage);
    List<Account> accountObjs = accountSV.list(accountObj, page.genRowStart(), page.getPageSize());
    int total = accountSV.count(accountObj);
    page.setTotalRow(total);
    log.info(JSON.toJSONString(page));
    return accountObjs;
  }

  /**
   * 查询Account信息集合
   *
   * @return 分页对象
   */
  @ApiOperation(value = "查询Account信息集合", notes = "查询Account信息集合")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
    @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
    @ApiImplicitParam(name = "account", value = "账户", paramType = "query")
  })
  @GetMapping(value = "/list/by")
  @ResponseBody
  public List<Account> listByPk(
      java.lang.Long id,
      java.lang.String code,
      java.lang.String account,
      Integer curPage,
      Integer pageSize) {
    Page<Account> page = new Page<Account>(pageSize, curPage);
    List<Account> accountObjs =
        accountSV.list(id, code, account, page.genRowStart(), page.getPageSize());
    int total = accountSV.count(id, code, account);
    page.setTotalRow(total);
    log.info(JSON.toJSONString(page));
    return accountObjs;
  }

  /**
   * 统计Account信息数量根据主键
   *
   * @return 总条数
   */
  @ApiOperation(value = " 统计Account信息数量根据主键", notes = " 统计Account信息数量根据主键")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
    @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
    @ApiImplicitParam(name = "account", value = "账户", paramType = "query")
  })
  @GetMapping(value = "/count/by")
  @ResponseBody
  public Integer count(java.lang.Long id, java.lang.String code, java.lang.String account) {
    return accountSV.count(id, code, account);
  }

  /**
   * 统计Account信息数量
   *
   * @return 总条数
   */
  @ApiOperation(value = "统计Account信息数量", notes = "统计Account信息数量")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
    @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
    @ApiImplicitParam(name = "account", value = "账户", paramType = "query"),
    @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
    @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
    @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
    @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
  })
  @PostMapping(value = "/count")
  @ResponseBody
  public Integer count(@RequestBody Account accountObj) {
    return accountSV.count(accountObj);
  }

  /**
   * 创建Account
   *
   * @return BeanRet
   */
  @ApiOperation(value = "创建Account", notes = "创建Account")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "account", value = "账户", required = true, paramType = "query"),
    @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query")
  })
  @PostMapping("/build")
  @ResponseBody
  public Account build(@RequestBody @ApiIgnore Account accountObj) {
    if (StringUtils.isBlank(accountObj.getAccount())) {
      return null;
    }
    if (StringUtils.isBlank(accountObj.getPassword())) {
      return null;
    }
    Account account = accountSV.loadByAccount(accountObj.getAccount());
    if (account != null) {
      return null;
    }
    accountSV.save(accountObj);
    return accountObj;
  }

  /**
   * 修改Account
   *
   * @return BeanRet
   */
  @ApiOperation(value = "修改Account", notes = "修改Account")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "account", value = "账户", paramType = "query"),
    @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
    @ApiImplicitParam(name = "state", value = "状态", paramType = "query")
  })
  @PutMapping("/modify")
  @ResponseBody
  public Account modify(@RequestBody @ApiIgnore Account accountObj) {
    accountSV.modify(accountObj);
    return accountObj;
  }

  /**
   * 删除Account
   *
   * @return BeanRet
   */
  @ApiOperation(value = "删除Account", notes = "删除Account")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
    @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
    @ApiImplicitParam(name = "account", value = "账户", paramType = "query")
  })
  @DeleteMapping("/delete")
  @ResponseBody
  public BeanRet delete(java.lang.Long id, java.lang.String code, java.lang.String account) {
    accountSV.delete(id, code, account);
    return BeanRet.create(true, "删除Account成功");
  }

  @ApiOperation(value = "根据账号和密码查询", notes = "根据账号和密码查询")
  @ApiImplicitParams({
    @ApiImplicitParam(
        name = "account",
        value = "account",
        paramType = "query",
        dataType = "String"),
    @ApiImplicitParam(
        name = "password",
        value = "password",
        paramType = "query",
        dataType = "String")
  })
  @RequestMapping(value = "/loadByAccountAndPwd", method = RequestMethod.GET)
  @ResponseBody
  public Account loadByAccountAndPwd(String account, String password) {
    if (StringUtils.isBlank(account)) {
      return null;
    }
    if (StringUtils.isBlank(password)) {
      return null;
    }
    Account accountObj = accountSV.loadByAccountAndPwd(account, password);
    return accountObj;
  }
}
